<?php

namespace app\admin\controller;

use think\exception\ValidateException;
use app\model\Composition;
use app\model\CompositionGoods;

use think\facade\Db;

class CompositionController extends Base
{

	function index()
	{
		$weid = weid();
		$keyword = input('post.keyword', '', 'serach_in');
		$query = Composition::where(['weid' => $weid]);
		if (!empty($keyword)) {
			$query->where('title', 'like', '%' . $keyword . '%');
		}

		$res = $query->order('sort asc,id desc')
			->paginate(getpage())
			->toArray();

		foreach ($res['data'] as &$vo) {
			$vo['created_time'] = time_format($vo['created_time']);
			$vo['updated_time'] = time_format($vo['updated_time']);
		}

		$data['data'] = $res;
		return $this->json($data);
	}

	function listUpdate()
	{
		$data = only('id,status,sort');
		if (!$data['id']) throw new ValidateException('参数错误');
		Composition::update($data);

		return $this->json(['msg' => '操作成功']);
	}

	public function update()
	{
		$id = $this->request->post('id');
		$data = input('post.');
		$composition['update_time'] = time();
		$composition['weid'] = weid();
		$composition['created_time'] = time();
		$composition['title'] = $data['title'];
		$composition['price'] = $data['price'];
		$composition['status'] = $data['status'];
		$composition['num'] = $data['num'];
		$composition['sort'] = $data['sort'];
		if (empty($data['goods'])) {
			throw new ValidateException('请选择服务');
		}
		Db::startTrans();
		if (empty($id)) {
			try {
				$res = Composition::create($composition);
				if ($res->id) {
					$data['id'] = $res->id;
					$this->_synupdata($data);
				}
				Db::commit();
			} catch (\Exception $e) {
				Db::rollback();
				throw new ValidateException($e->getMessage());
			}

			return $this->json(['msg' => '添加成功', 'data' => $res->id]);
		} else {

			try {
				$composition['id'] = $id;
				Composition::update($composition);
				$data['id'] = $id;
				$this->_synupdata($data);
				Db::commit();
			} catch (\Exception $e) {
				Db::rollback();
				throw new ValidateException($e->getMessage());
			}
			return $this->json(['msg' => '修改成功']);
		}
	}
	function _synupdata($data)
	{
		if (isset($data['goods'])) {
			foreach ($data['goods'] as $val) {
				CompositionGoods::where(['composition_id' => $data['id'], 'goods_id' => $val['id']])->delete();
				CompositionGoods::create([
					'composition_id' => (int) $data['id'],
					'goods_id' => (int) $val['id'],
					'is_main' => (int) $val['is_main'],
				]);
			}
		}
	}

	function getInfo()
	{
		$id =  $this->request->post('id', '', 'serach_in');
		if (!$id) throw new ValidateException('参数错误');
		$data = Composition::field('*')->with(['goods'])->find($id);
		$goods = CompositionGoods::setGoodslist($data['goods']);
		unset($data['goods']);
		usort($goods, function($a, $b) {
			return $b['is_main'] <=> $a['is_main']; // 使用太空船操作符
		});
		$data['goods'] = $goods;
		
		return $this->json(['data' => $data]);
	}

	function delete()
	{
		$idx =  $this->request->post('id', '', 'serach_in');
		if (!$idx) throw new ValidateException('参数错误');
		Composition::destroy(['id' => explode(',', $idx)], true);
		CompositionGoods::destroy(['composition_id' => explode(',', $idx)], true);
		return $this->json(['msg' => '操作成功']);
	}
}
